/*
 * Copyright (C) 2017 WordPlat Open Source Project
 *
 *      https://wordplat.com/InteractiveKLineView/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.ikvstockchart.drawing;


import com.wordplat.ikvstockchart.entry.SizeColor;
import com.wordplat.ikvstockchart.render.AbstractRender;
import com.wordplat.ikvstockchart.utils.StringUtils;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.agp.utils.TextAlignment;

/**
 * <p>EmptyDataDrawing</p>
 * <p>Date: 2017/3/21</p>
 *
 * @author afon
 * @since 2021-05-09
 */
public class EmptyDataDrawing implements IDrawing {
    private Paint textPaint;
    private final Paint.FontMetrics fontMetrics = new Paint.FontMetrics(0, 0, 0, 0, 0);

    private AbstractRender render;
    private SizeColor sizeColor;
    private final RectFloat contentRect = new RectFloat();

    @Override
    public void onInit(RectFloat rectFloat, AbstractRender abstractRender) {
        this.render = abstractRender;

        this.contentRect.left = rectFloat.left;
        this.contentRect.top = rectFloat.top;
        this.contentRect.bottom = rectFloat.bottom;
        this.contentRect.right = rectFloat.right;

        this.sizeColor = abstractRender.getSizeColor();

        if (textPaint == null) {
            textPaint = new Paint();
            textPaint.setAntiAlias(true);
        }
        textPaint.setTextAlign(TextAlignment.CENTER);
    }

    @Override
    public void computePoint(int minIndex, int maxIndex, int currentIndex) {

    }

    @Override
    public void onComputeOver(Canvas canvas, int minIndex, int maxIndex, float minY, float maxY) {

    }

    @Override
    public void onDrawOver(Canvas canvas) {
        if (render.getEntrySet().getEntryList().size() == 0) {
            final String drawText;
            if (render.getEntrySet().isLoadingStatus()) {
                textPaint.setTextSize(StringUtils.floatToInt(sizeColor.getLoadingTextSize()));
                textPaint.setColor(new Color(sizeColor.getLoadingTextColor()));
                drawText = sizeColor.getLoadingText();
            } else {

                textPaint.setTextSize(StringUtils.floatToInt(sizeColor.getErrorTextSize()));
                textPaint.setColor(new Color(sizeColor.getErrorTextColor()));
                drawText = sizeColor.getErrorText();
            }
            textPaint.getFontMetrics();

            canvas.drawText(textPaint, drawText,
                    contentRect.getWidth() / 2,
                    (contentRect.top + contentRect.bottom - fontMetrics.top - fontMetrics.bottom) / 2);
        }
    }
}
